package com.example.mvvmdemo.viewmodel;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.LiveData;

import com.example.mvvmdemo.MyApplication;
import com.example.mvvmdemo.db.UserDao;
import com.example.mvvmdemo.db.UserDatabase;
import com.example.mvvmdemo.model.User;
import com.example.mvvmdemo.repository.UserRepository;

/**
 * @author: tuyrt7 on 2020/8/31.
 * Describe:
 */
public class UserViewModel extends AndroidViewModel {

    private LiveData<User> user;
    private UserRepository mUserRepository;
    private String userName = "tuyrt7";

    public UserViewModel(@NonNull Application application) {
        super(application);
        UserDatabase database = MyApplication.getUserDatabase();
        UserDao userDao = database.userDao();
        mUserRepository = new UserRepository(userDao, MyApplication.getApi());
        user = mUserRepository.getUser(userName);
    }

    public LiveData<User> getUser() {
        return user;
    }

    public void refresh() {
        mUserRepository.refresh(userName);
    }
}
